package com.zhilianbao.leyaogo.view.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhilianbao.leyaogo.R;


public class AddOrMinusView extends FrameLayout implements android.view.View.OnClickListener {
	private TextView tvCountShower, tvTitle;
	private int totalCount = 1;
	private int stepCount = 1;
	private Object data;
	private OnPrepareCountAddOrMinusListener mAddOrMinusListener;
	private ImageView mMinIv,mAddIv;
	private boolean  addRespond = true,subRespond = true;

	public AddOrMinusView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public AddOrMinusView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public AddOrMinusView(Context context) {
		this(context, null);
	}

	private void init(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.layout_view_add_or_minus, this);
		tvTitle = (TextView) view.findViewById(R.id.tv_title);
		tvCountShower = (TextView) view.findViewById(R.id.tv_count);
		mMinIv = (ImageView) view.findViewById(R.id.iv_minus);
		mMinIv.setOnClickListener(this);
		mAddIv = (ImageView) view.findViewById(R.id.iv_add);
		mAddIv.setOnClickListener(this);
		setDefaultCount(0);
	}

	public void setDefaultCount(int defaultcount) {
		add();
	}

	public void setTagData(Object data) {
		this.data = data;
	}

	public Object getTagData() {
		return data;
	}

	public void setTitle(String title) {
		if (!TextUtils.isEmpty(title)) {
			tvTitle.setText(title);
			tvTitle.setVisibility(View.VISIBLE);
		}
	}

	public void setCountShowerTvColor(int resId){
		tvCountShower.setTextColor(resId);
	}

	public void setShowerCount(int count){
		tvCountShower.setText(String.valueOf(count));
		totalCount = count;
	}

	public void setMinIvResource(int resId){
		mMinIv.setImageResource(resId);
	}
	public void setAddIvResource(int resId){
		mAddIv.setImageResource(resId);
	}
	public void setStepCount(int stepCount) {
		this.stepCount = stepCount;
	}

	public void setOnPrepareCountAddOrMinusListener(OnPrepareCountAddOrMinusListener mAddOrMinusListener) {
		this.mAddOrMinusListener = mAddOrMinusListener;
	}

	public void setAddRespond(boolean respond){
		this.addRespond = respond;
	}

	public void setSubRespond(boolean respond){
		this.subRespond = respond;
	}

	public int getCount() {
		return totalCount;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_minus:
			minus();
			break;
		case R.id.iv_add:
			add();
			break;

		default:
			break;
		}
	}

	private void add() {
		if (mAddOrMinusListener != null && mAddOrMinusListener.prepareAdd(data, totalCount, totalCount + stepCount,addRespond)) {
//			totalCount += stepCount;
//			tvCountShower.setText(totalCount + "");
		}
	}

	private void minus() {
		if (mAddOrMinusListener != null && mAddOrMinusListener.prepareMinus(data, totalCount, totalCount - stepCount,subRespond)) {
//			totalCount -= stepCount;
//			tvCountShower.setText(totalCount + "");
		}
	}

	public interface OnPrepareCountAddOrMinusListener {
		boolean prepareAdd(Object data,int currentCount, int nextCount,boolean respond);
		boolean prepareMinus(Object data,int currentCount, int nextCount,boolean respond);
	}

}
